//
//  ByCellAppointmentListItem.m
//  mijifen_ios
//
//  Created by 朱凯 on 2017/12/12.
//  Copyright © 2017年 itboye. All rights reserved.
//

#import "ByCellAppointmentListItem.h"

// view
#import "ByViewSelectServiceHaved.h"

@implementation ByCellAppointmentListItem
{
    UILabel     *_dateLabel;        // 日期
    UILabel     *_countLabel;       // 人数
    UIButton    *_statusView;       // 状态
    UIButton    *_appointButton;    // 预约
}
    
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setupUI];
    }
    return self;
}
- (void)setupUI {
    
    _dateLabel = [[UILabel alloc] init];
    _dateLabel.adjustsFontSizeToFitWidth = true;
    _dateLabel.numberOfLines = 2;
    _dateLabel.text = @"2017.12.12\n（周二）";
    _dateLabel.font = FONT(14);
    _dateLabel.textAlignment = NSTextAlignmentLeft;
    _dateLabel.textColor = [UIColor grayColor];
    [self.contentView addSubview:_dateLabel];
    
    _countLabel = [[UILabel alloc] init];
    _countLabel.adjustsFontSizeToFitWidth = true;
    _countLabel.text = @"预约人数:40";
    _countLabel.font = FONT(14);
    _countLabel.textAlignment = NSTextAlignmentRight;
    _countLabel.textColor = [UIColor grayColor];
    [self.contentView addSubview:_countLabel];
    
    _statusView = [UIButton buttonWithType:UIButtonTypeCustom];
    _statusView.titleLabel.font = FONT(14);
    _statusView.userInteractionEnabled = false;
    [_statusView setTitle:@"可预约" forState:UIControlStateNormal];
    [_statusView setTitle:@"预约已满" forState:UIControlStateSelected];
    [_statusView setTitle:@"不可预约" forState:UIControlStateDisabled];
    [_statusView setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
    [_statusView setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
    [_statusView setTitleColor:[UIColor lightGrayColor] forState:UIControlStateDisabled];
    [self.contentView addSubview:_statusView];
    
    _appointButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _appointButton.layer.cornerRadius = 6;
    _appointButton.layer.masksToBounds = true;
    [_appointButton setBackgroundImage:[UIImage imageWithColor:rgba(149, 214, 250, 1)] forState:UIControlStateNormal];
    [_appointButton setBackgroundImage:[UIImage imageWithColor:[UIColor lightGrayColor]] forState:UIControlStateSelected];
    [_appointButton setBackgroundImage:[UIImage imageWithColor:[UIColor lightGrayColor]] forState:UIControlStateDisabled];
    _appointButton.titleLabel.font = FONT(14);
    [_appointButton setTitle:@"确定预约" forState:UIControlStateNormal];
    [_appointButton setTitle:@"预约结束" forState:UIControlStateSelected];
    [_appointButton setTitle:@"预约结束" forState:UIControlStateDisabled];
    [_appointButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_appointButton addTarget:self action:@selector(appoint) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_appointButton];
    
    // 线条
    UIView *line = [UIView new];
    line.backgroundColor = [UIColor colorWithWhite:0.90 alpha:1];
    [self.contentView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self);
        make.height.equalTo(@3);
    }];
    
    [_appointButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(75, 30));
        make.centerY.equalTo(@0);
        make.right.equalTo(@-10);
    }];
    [_dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@10);
        make.centerY.equalTo(@0);
        make.width.equalTo(self.contentView).multipliedBy(0.2);
        make.height.equalTo(self.contentView);
    }];
    [_countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView.mas_centerX);
        make.centerY.equalTo(@0);
        make.left.equalTo(_dateLabel.mas_right);
    }];
    [_statusView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(@0);
        make.left.equalTo(_countLabel.mas_right);
        make.right.equalTo(_appointButton.mas_left);
    }];
}

- (void)appoint {
    
    ByViewSelectServiceHaved *view = [ByViewSelectServiceHaved new];
    [view show];
    

}

- (void)setTestTag:(NSInteger)testTag {
    
    _testTag = testTag;
    
    _statusView.enabled = testTag & (1 << 0);
    if (_statusView.enabled) {
        _statusView.selected = testTag & (1 << 1);
    }
    _appointButton.enabled = testTag & (1 << 2);
    if (_appointButton.enabled) {
        _appointButton.selected = testTag & (1 << 3);
    }
}

@end
